package cn.fldong.employee.servlet;

import cn.fldong.employee.dao.EmployeeDao;
import cn.fldong.employee.entity.EmployeeEntity;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

@WebServlet("/update")
public class UpdateEmployeeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String empID= req.getParameter("empID");
        String message=null;
        //根据员工号判断是否已经存在员工信息
        EmployeeDao dao=new EmployeeDao();
        EmployeeEntity result=dao.queryByEmpID(empID);
        if (result==null){
            message = "操作无效，员工不存在！";
            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();
            out.println("<html><body>");
            out.println("<h1>" + message + "</h1>");
            out.println("<h2><a href='update.jsp'>"+"返回"+"</a></h2>");
            out.println("</body></html>");
            out.close();
        }else{
            EmployeeEntity employee = dao.queryByEmpID(empID);
            req.setAttribute("employee", employee);
            req.getRequestDispatcher("/updateinfo.jsp").forward(req, resp);
        }
    }
}
